<?php
//自动生成,请根据需要修改
namespace app\activity\controller;

use app\common\controller\Base;
use think\Db;
use think\Validate;
use think\db\Where;

/**
 * @title 活动报名页面（模块09-02）
 * @description 活动报名说明
 * @group 活动（09）
 * @header name:model_id require:0 default:09-02 desc:模块
 * @groupremark 已使用错误码：1-09-02-0001 校验失败 1-09-02-0002 添加失败 1-09-02-0003 编辑校验失败 1-09-02-0004 编辑失败 1-09-02-0005 获取详情失败  1-09-02-0006 获取列表失败 1-09-02-0007获取单个详情失败 1-09-02-0008 删除失败
 */
class ActivityParticipate extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加活动报名
     * @description 添加活动报名
     * @author 李锐志
     * @url /activity/activity_participate/add
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:user_name type:varchar require:0 other: desc:用户名
     * @param name:user_phone type:varchar require:0 other: desc:用户手机
     * @param name:activity_id type:int require:0 other: desc:活动id
     * @param name:status type:tinyint require:0 other: desc:状态
     * @param name:create_time type:tinyint require:0 other: desc:创建时间
     * @return data:活动报名@!
     * @data    id:id    user_id:用户id    user_name:用户名    user_phone:用户手机    activity_id:活动id    status:状态
     * @return error_id:1-09-02-0001 校验失败  1-09-02-0002 添加失败
     */
    public function add()
    {
        $data = input();
        $data['create_time'] = time();
        /**
         *$validate = new \app\activity\validate\ActivityParticipate();
         *$validate->scene('add');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-09-02-0001';
         *return $this->errorJson($msg);
         *}
         **/
        $model = new \app\activity\model\ActivityParticipate();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-09-02-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑活动报名
     * @description 编辑活动报名
     * @author 李锐志
     * @url /activity/activity_participate/edit
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:user_name type:varchar require:0 other: desc:用户名
     * @param name:user_phone type:varchar require:0 other: desc:用户手机
     * @param name:activity_id type:int require:0 other: desc:活动id
     * @param name:status type:tinyint require:0 other: desc:状态
     * @param name:create_time type:tinyint require:0 other: desc:创建时间
     * @return data:活动报名@!
     * @data    id:id    user_id:用户id    user_name:用户名    user_phone:用户手机    activity_id:活动id    status:状态
     * @return error_id:1-09-02-0003 校验失败 1-09-02-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $validate = new \app\activity\validate\ActivityParticipate();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-09-02-0003';
            return $this->errorJson($msg);
        }
        $model = new \app\activity\model\ActivityParticipate();
        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-09-02-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除活动报名
     * @description 删除活动报名
     * @author 李锐志
     * @url /activity/activity_participate/delete
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-09-02-0008 删除失败
     */
    public function delete()
    {
        $id = input('id');
        $model = new \app\activity\model\ActivityParticipate();
        $flag = $model->destroy($id);
        if ($flag) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-09-02-0008';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 单个查询活动报名
     * @description 单个查询活动报名
     * @author 李锐志
     * @url /activity/activity_participate/info
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:user_id type:int require:0 other: desc:用户id
     * @param name:user_name type:varchar require:0 other: desc:用户名
     * @param name:user_phone type:varchar require:0 other: desc:用户手机
     * @param name:activity_id type:int require:0 other: desc:活动id
     * @param name:status type:tinyint require:0 other: desc:状态
     * @param name:create_time type:tinyint require:0 other: desc:创建时间
     * @return data:活动报名@!
     * @data    id:id    user_id:用户id    user_name:用户名    user_phone:用户手机    activity_id:活动id    status:状态
     * @return error_id:1-09-02-0005 校验失败 1-09-02-0007 获取失败
     */
    public function info()
    {
        $model = new \app\activity\model\ActivityParticipate();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\activity\validate\ActivityParticipate();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-09-02-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\activity\model\ActivityParticipate();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-09-02-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表活动报名
     * @description 查询列表活动报名
     * @author 李锐志
     * @url /activity/activity_participate/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:活动报名@
     * @list    id:id    user_id:用户id    user_name:用户名    user_phone:用户手机    activity_id:活动id    status:状态
     * @return error_id:1-09-02-0006 校验失败
     */
    public function get_list($export = '')
    {
        $model = new \app\activity\model\ActivityParticipate();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        foreach($data as $k => $v){
            if($v === 0){
                continue;
            }
            if($v == null){
                unset($data[$k]);
            }
        }
        if(isset($data['user_name']) && $data['user_name'] && !empty($data['user_name'])){
            $data['user_name'] = ['like',"%{$data['user_name']}%"];
        }
        if(isset($data['user_phone']) && $data['user_phone'] && !empty($data['user_phone'])){
            $data['user_phone'] = ['like',"%{$data['user_phone']}%"];
        }
        if(isset($data['activity_id']) && $data['activity_id'] && !empty($data['activity_id'])){
            $activity_map['title'] = array('like', "%{$data['activity_id']}%");//活动名
            $activity_ids = Db::table('zb_activity.tp_activity a')->where(new Where($activity_map))->column('id');
            $data['activity_id'] = array('in', $activity_ids);

        }
        /**
         *$validate = new \app\activity\validate\ActivityParticipate();
         *$validate->scene('lists');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-09-02-0006';
         *return $this->errorJson($msg);
         *}
         **/
        if (empty($export)){
            $data = new Where($data);
            $list = $model->where($data)->order('id','desc')->paginate();
            foreach ($list as $k => $v) {
                $list[$k]['title'] = Db::table('zb_activity.tp_activity a')->where('a.id','=',$v['activity_id'])
                    ->value('a.title');
            }
            $msg['list'] = $list;
            return $this->successJson($msg);
        }else{
            $list = $model->field('id,user_id,user_name,user_phone,status,create_time,activity_id')->order('id','desc')->paginate();
            foreach ($list as $k => $v) {
                $list[$k]['title'] = Db::table('zb_activity.tp_activity a')
                    ->where('a.id','=',$v['activity_id'])
                    ->value('a.title');
                unset( $list[$k]['activity_id']);
                unset( $list[$k]['user_id']);
            }
            return $list;
        }

    }

    /**
     * @title 前台活动报名
     * @description 前台活动报名
     * @author 李锐志
     * @url /activity/activity_participate/activity_apply
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:activity_id require:1 default: desc:activity_id
     * @param name:name require:1 default: desc:name
     * @param name:phone require:1 default: desc:phone
     */
    public function activity_apply()
    {
        $data = input();

        $validate = new Validate([
            'token' => 'require',
            'activity_id' => 'require',
            'name' => 'require',
            'phone' => 'require',
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        $over_time = Db::name('activity')
            ->where('id',$data['activity_id'])
            ->where('over_time','<',time())
            ->find();
        if ($over_time) {
            $msg['msg'] = '活动报名时间已结束';
            return $this->errorJson($msg);
        }

        $activity = Db::name('activity')
            ->where('id', $data['activity_id'])
            ->where('status', 1)
            ->find();
        if (!$activity) {
            $msg['msg'] = '活动不存在或活动已取消';
            return $this->errorJson($msg);
        }

        $flag = Db::name('activity_participate')
            ->where('user_id', USERID)
            ->where('activity_id', $data['activity_id'])
            ->find();
        if ($flag) {
            $msg['msg'] = '不可重复报名!';
            return $this->errorJson($msg);
        }

        $result = Db::name('activity_participate')
            ->insert([
                'user_id' => USERID,
                'user_name' => $data['name'],
                'user_phone' => $data['phone'],
                'activity_id' => $data['activity_id'],
                'create_time' => time()
            ]);

        if ($result) {
            $msg['msg'] = '报名成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '报名失败';
            return $this->errorJson($msg);
        }
    }
    /**
     * 后台轮播图启用按钮
     */
    public function invoke()
    {
        $data = input();

        Db::name('activity_participate')
            ->where('id',$data['id'])
            ->setField('status',1);

        $msg['msg'] = '轮播图启用成功';
        return $this->successJson($msg);
    }

    /**
     * 后台轮播图禁用按钮
     */
    public function forbidden()
    {
        $data = input();

        Db::name('activity_participate')
            ->where('id',$data['id'])
            ->setField('status',0);

        $msg['msg'] = '轮播图禁用成功';
        return $this->successJson($msg);
    }
    /**
     * 导出数据到excel
     */
    public function export_data()
    {
        $data = input();
        $queryCondition = empty($data['queryCondition']) ? array('sys_sign' => 'base') : $data['queryCondition'];
        $orderData = $this->get_list($queryCondition);
        $orderData = $orderData->toArray();
        $name = date('Y年m月d日H时i分s秒', time()) . '-参与活动用户';

        if (!empty($orderData)) {
            foreach($orderData['data'] as $k => $v){
                $orderData['data'][$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                if($v['status'] == 1){
                    $orderData['data'][$k]['status'] = '启用';
                }else{
                    $orderData['data'][$k]['status'] = '禁用';
                }
            }
            $keys = array_keys($orderData['data'][0]);
            $head = array_values(array_merge(array_flip($keys), $data['header'][0]));//表头根据字段排序

        }
        array_splice($head, 3, 1);
        array_splice($keys, 3, 1);
        $msg['header'] = $head;
        $msg['data'] = $orderData['data'];
        $msg['name'] = $name;
        $msg['keys'] = $keys;
        $msg['total'] = count($orderData['data']);
        return $this->successJson($msg);
    }
}

